<template>
  <a-modal
    class="noborder"
    :title="title"
    :width="520"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="Cancel"
    okText="OK"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="기준 메일 주소" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-model="oldEmail" disabled/>
        </a-form-item>
        <a-form-item label="새 메일 주소" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['email', { rules: [{ required: true, message: '새 메일 주소 입력하십시오.' }] }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
// import pick from 'lodash.pick'
import * as Api from '@/api/user'

// 充值类型枚举: 余额
const RECHARGE_TYPE_BALANCE = 'balance'

// 充值类型枚举: 积分
const RECHARGE_TYPE_POINTS = 'points'

export default {
  components: {},
  props: {
    // 会员等级列表
    oldEmail: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 对话框标题
      title: '메일 수정',
      // 标签布局属性
      labelCol: { span: 8 },
      // 输入框布局属性
      wrapperCol: { span: 15 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前tab索引
      activeKey: RECHARGE_TYPE_BALANCE,
      // 充值类型
      RECHARGE_TYPE_BALANCE,
      RECHARGE_TYPE_POINTS,
      // 当前记录
      record: {},
    }
  },
  methods: {
    // 显示对话框
    handle(record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
    },

    // 切换tabs事件
    onChangeTabs(activeKey) {
      this.activeKey = activeKey
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      const { record, activeKey } = this
      console.log(values,'这是啥')
      this.confirmLoading = true
      Api.eamilEdit({
        user_id: record.user_id,
        email: values.email,
      })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
  },
}
</script>
<style lang="less" scoped>
.ant-modal-body {
  padding-top: 0 !important;
}
.ant-form-item {
  margin-bottom: 15px;
}
.ant-tabs-nav .ant-tabs-tab {
  padding: 10px 16px;
}
</style>
